package me.panpf.sketch.drawable;

import me.panpf.sketch.decode.ImageAttrs;
import me.panpf.sketch.util.SketchUtils;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class SketchBitmap {

    @NotNull
    private String key;
    @NotNull
    private String uri;
    @Nullable
    protected PixmapBuild pixmapBuild;
    @NotNull
    private ImageAttrs attrs;

    protected SketchBitmap(@NotNull PixmapBuild imageSource, @NotNull String key, @NotNull String uri, @NotNull ImageAttrs attrs) {
        //noinspection ConstantConditions
        if (imageSource == null || imageSource.getPixelMap().isReleased()) {
            throw new IllegalArgumentException("bitmap is null or recycled");
        }

        this.pixmapBuild = imageSource;
        this.key = key;
        this.uri = uri;
        this.attrs = attrs;
    }

    @Nullable
    public ImageSource getImageSource() {
        return pixmapBuild.getImageSource();
    }

    public PixmapBuild getPixmapBuild() {
        return pixmapBuild;
    }

    @NotNull
    public String getKey() {
        return key;
    }

    @NotNull
    public String getUri() {
        return uri;
    }

    @NotNull
    public ImageAttrs getAttrs() {
        return attrs;
    }

    @NotNull
    public abstract String getInfo();

    public long getByteCount() {
        try {
            return SketchUtils.getByteCount(pixmapBuild.getPixelMap());
        } catch (Exception e) {
            return 0;
        }
    }

    @Nullable
    public PixelFormat getBitmapConfig() {
        return pixmapBuild != null ? pixmapBuild.getPixelMap().getImageInfo().pixelFormat : null;
    }
}
